package com.bootdo.system.dao;

import com.bootdo.system.domain.Address;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface AddressMapper {

	@Select("select * from tb_address where id = #{id}")
	Address get(Long id);
	
	@Select("<script>" +
	"select * from tb_address " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"linkerName != null and linkerName != ''\">"+ "and linkerName = #{linkerName} " + "</if>" + 
	  		  "<if test=\"addressType != null and addressType != ''\">"+ "and addressType = #{addressType} " + "</if>" + 
	  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" + 
		  	"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<Address> list(Map<String,Object> map);
	
	@Select("<script>" +
	"select count(*) from tb_address " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"linkerName != null and linkerName != ''\">"+ "and linkerName = #{linkerName} " + "</if>" + 
	  		  "<if test=\"addressType != null and addressType != ''\">"+ "and addressType = #{addressType} " + "</if>" + 
	  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" +  
		  	"</where>"+ 
			"</script>")
	int count(Map<String,Object> map);
	
	@Insert("insert into tb_address (`linkerName`, `linkerSex`, `linkerPhone`, `linkAddress`, `addressType`, `ownerId`,`longitude`, `latitude`,  `createUserId`, `createTime`)"
	+ "values (#{linkerName}, #{linkerSex}, #{linkerPhone}, #{linkAddress}, #{addressType}, #{ownerId},#{longitude},#{latitude},#{createUserId}, #{createTime})")
	@Options(useGeneratedKeys=true)
	int save(Address bComments);
	
	@Update("<script>"+ 
			"update tb_address " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"linkerName != null\">`linkerName` = #{linkerName}, </if>" + 
                    "<if test=\"linkerSex != null\">`linkerSex` = #{linkerSex}, </if>" + 
                    "<if test=\"linkerPhone != null\">`linkerPhone` = #{linkerPhone}, </if>" + 
                    "<if test=\"linkAddress != null\">`linkAddress` = #{linkAddress}, </if>" + 
                    "<if test=\"addressType != null\">`addressType` = #{addressType}, </if>" +    
                    "<if test=\"ownerId != null\">`ownerId` = #{ownerId}, </if>" +    
                    "<if test=\"longitude != null\">`longitude` = #{longitude}, </if>" +    
                    "<if test=\"latitude != null\">`latitude` = #{latitude}, </if>" +    
          			"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(Address bComments);
	
	@Delete("delete from tb_address where id =#{id}")
	int remove(Long id);
	
	@Delete("<script>"+ 
			"delete from tb_address where id in " + 
					"<foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">" + 
						"#{id}" + 
					"</foreach>"+
			"</script>")
	int batchRemove(Integer[] ids);
}
